package org.jeecg.modules.educational.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.jeecg.modules.education.entity.Education;
import org.jeecg.modules.educational.entity.EducationMessage;
import org.jeecg.modules.educational.entity.vo.EducationMessageVO;
import org.jeecg.modules.educational.mapper.EducationMessageMapper;
import org.jeecg.modules.educational.service.EducationMessageService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.modules.internship.entity.Internship;
import org.jeecg.modules.internship.service.InternshipService;
import org.jeecg.modules.system.entity.SysDict;
import org.jeecg.modules.system.entity.SysDictItem;
import org.jeecg.modules.system.service.ISysDictItemService;
import org.jeecg.modules.system.service.ISysDictService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author liujiamiao
 * @since 2020-11-06
 */
@Service
public class EducationMessageServiceImpl extends ServiceImpl<EducationMessageMapper, EducationMessage> implements EducationMessageService {

    @Autowired
    private InternshipService internshipService;

    @Autowired
    private ISysDictService iSysDictService;
    @Autowired
    private ISysDictItemService iSysDictItemService;

    //该字段为数据字典，表中设置的数据字典与之是同一个值
    @Value("${educational.learningForm}")
    private String fieldName;

    private Integer count = 0;

    /**
     * 分页查询
     * @param page
     * @param limit
     * @param basicId
     * @return
     */
    @Override
    public IPage<EducationMessage> pageSelect(Integer page, Integer limit, Integer basicId) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("basic_id",basicId);
        Page<EducationMessage> educationMessagePage = new Page<>(page, limit);
        IPage<EducationMessage> iPage = this.page(educationMessagePage, wrapper);

        Internship basic = internshipService.getById(basicId);
        count = (page-1)*limit;

        List<EducationMessage> collect = iPage.getRecords().stream().map(entity -> {
            EducationMessageVO vo = new EducationMessageVO();
            BeanUtils.copyProperties(entity, vo);
            //名字
            vo.setName(basic.getName());
            //设置序列
            vo.setSerialNumber(++count);
            //是否是最高学历
            vo.setIsMaxEducationString(vo.getIsMaxEducation()==true?"是":"否");
            vo.setIsFullTime("否");

            if(fieldName!=null){
                //根据数据字典获取该行
                QueryWrapper<SysDict> sysDictQueryWrapper = new QueryWrapper<>();
                sysDictQueryWrapper.eq("dict_code",fieldName);
                SysDict dict = iSysDictService.getOne(sysDictQueryWrapper);
                if(dict!=null){
                    //获取全日制的itemValue
                    QueryWrapper<SysDictItem> sysDictItemQueryWrapper = new QueryWrapper<>();
                    sysDictItemQueryWrapper.eq("dict_id",dict.getId());
//                    sysDictItemQueryWrapper.eq("item_text","全日制");
                    List<SysDictItem> sysDictItemList = iSysDictItemService.list(sysDictItemQueryWrapper);
                    if(sysDictItemList!=null){

                        for(SysDictItem sysDictItem:sysDictItemList){
                            if(sysDictItem.getItemValue().equals(vo.getLearningForm())){
                                //学习形式
                                vo.setLearningForm(sysDictItem.getItemText());
                                //判断是否是全日制
                                if("全日制".equals(sysDictItem.getItemText())){
                                    vo.setIsFullTime("是");
                                }
                            }
                        }
                    }

                }
            }
            return vo;
        }).collect(Collectors.toList());
        iPage.setRecords(collect);
        return iPage;
    }
}
